package com.zeny.coding.controller.admin.business;


import com.zeny.coding.constants.CourseConstants;
import com.zeny.coding.dto.CourseContentFileDto;
import com.zeny.coding.dto.Response;
import com.zeny.coding.service.CourseContentFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.xml.bind.ValidationException;
import java.util.List;

/**
 * <h2>课程内容文件</h2>
 *
 * @Author Zeny
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/business/admin/courseContentFile")
public class CourseContentFileController {

    @Autowired
    private CourseContentFileService courseContentFileService;

    /**
     * <h2>根据课程id查找课程文件</h2>
     * @param courseId
     **/
    @GetMapping("/list/{courseId}")
    public Response list(@PathVariable("courseId") String courseId) {
        List<CourseContentFileDto> courseContentFileDtoList = courseContentFileService.list(courseId);
        return new Response().success().data(courseContentFileDtoList).message(CourseConstants.QUERY_SUCCESS);
    }

    @PostMapping("/save")
    public Response save(@RequestBody CourseContentFileDto courseContentFileDto) throws ValidationException {
        courseContentFileDto.isValidForSave();
        this.courseContentFileService.save(courseContentFileDto);
        return new Response().success().message(CourseConstants.SAVE_SUCCESS);
    }


    @DeleteMapping("/deleteById/{id}")
    public Response deleteById(@PathVariable("id") String id) {
        if (StringUtils.isEmpty(id)) {
            return new Response().fail().message(CourseConstants.REQUEST_PARAMS_ERROR);
        }
        this.courseContentFileService.deleteById(id);
        return new Response().success().message(CourseConstants.DELETE_SUCCESS);
    }
}
